#include "thread.h"


struct ThreadData {
    int threadId;
    int vid;//vid
    int pid;//pid
    int serialportnum;//几号串口
    QString route;//位置路径
    bool isstart;//是否开启
    // 其他你想要传递的数据成员
};
struct ServiceReturnModel{
    int slot ;
    int devID;
    int battery;
    bool umsStatue;
    QString snstring ;
    QString version;
};
// 实现 WorkThread
WorkThread::WorkThread()
{
    qDebug() <<"WorkThread";
}

// 实现 WorkThread
WorkThread::WorkThread(ThreadPool *parent)
{
    qDebug() <<"WorkThread(ThreadPool *parent)";
    task=new Task();
    m_Pool = parent;

}

WorkThread::~WorkThread()
{
    qDebug() << __FUNCTION__ << m_Handle << "Thread quit: " << isFinished();
    delete task;
    delete m_Pool;
    qDebug() <<"~WorkThread";
}


void WorkThread::run()
{
    m_Handle = QThread::currentThreadId();
    //    if(m_Pool->Name=="C6")//记录线程编号
    //    {
    //        C6Thread[C6Num]=m_Handle;
    //        C6Num++;
    //    }else if(m_Pool->Name=="C7")
    //    {
    //        C7Thread=m_Handle;
    //    }else if(m_Pool->Name=="Upload")
    //    {
    //        UploadThread=m_Handle;
    //    }else if(m_Pool->Name=="Upload_Two")
    //    {
    //        UploadTwoThread=m_Handle;
    //    }else if(m_Pool->Name=="Heartbeat")
    //    {
    //        HeartbeatThread[HeartbeatNum]=m_Handle;
    //        HeartbeatNum++;
    //    }/*else if(m_Pool->Name=="S8")
    //    {
    //        C7Thread=m_Handle;
    //    }*/

    while (!m_Pool->m_bStop)
    {
        m_Pool->m_TaskMutex.lock();
        while (m_Pool->m_Tasks.isEmpty())
        {
            qDebug() << QThread::currentThreadId() << "进入等待";
            //阻塞
            m_Pool->m_NotEmpty.wait(&m_Pool->m_TaskMutex);
            qDebug() << QThread::currentThreadId() << "被唤醒";

            // 线程退出条件判断
            if (m_Pool->m_bStop)
            {
                qDebug() << QThread::currentThreadId() << "6";
                m_Pool->m_TaskMutex.unlock();
                return;
            }
        }

        qDebug() << "取任务";
        // 取出任务
        task->Indet = m_Pool->m_Tasks.front().Indet;
        task->ParentID = m_Pool->m_Tasks.front().ParentID;
        task->SubID = m_Pool->m_Tasks.front().SubID;
        task->Data = m_Pool->m_Tasks.front().Data;

        m_Pool->m_Tasks.pop_front();
        m_Pool->m_TaskMutex.unlock();
        task->execute(task->Indet);
    }
}
// 实现 ThreadPool
ThreadPool::ThreadPool()
{
    qDebug() <<"ThreadPool";
    // ThreadNum=0;
}

// 实现 ThreadPool
ThreadPool::ThreadPool(int count, QObject *parent) : QObject(parent), m_bStop(false)
{
    qDebug() <<"ThreadPool(int count, QObject *parent) : QObject(parent), m_bStop(false)";
    for (int i = 0; i < count; i++)
    {

        m_Threads.push_back(QSharedPointer<WorkThread>(new WorkThread(this)));
    }
}

ThreadPool::~ThreadPool()
{
    qDebug() <<"~ThreadPool()";
    Destroy();
}

void ThreadPool::PushTask(ThreadInfo threadinfo)
{
    //上锁
    m_TaskMutex.lock();
    //尾插
    m_Tasks.push_back(threadinfo);
    // m_NotEmpty.wakeOne();
    //解锁
    m_TaskMutex.unlock();
}
void ThreadPool::PushTask(ThreadInfo threadinfo,bool IsNotEmpty)
{
    //上锁
    m_TaskMutex.lock();
    //尾插
    m_Tasks.push_back(threadinfo);
    m_NotEmpty.wakeOne();
    //解锁
    m_TaskMutex.unlock();
}

void ThreadPool::Start(QString threadname)
{
    Name=threadname;

    for (auto thread : m_Threads)
    {
        thread->start();

        // 创建定时器
    }

}

void ThreadPool::Destroy()
{
    m_bStop = true;
    m_NotEmpty.wakeAll();

    // 等待线程退出
    for (auto thread : m_Threads)
    {
        if (!thread->isFinished())
        {
            //thread->terminate();//FDP
            thread->wait();
        }
    }
    // 清空智能指针自动释放
    m_Threads.clear();
}
void ThreadPool::a()
{
    m_NotEmpty.wakeAll();

    // 等待线程退出
    for (auto thread : m_Threads)
    {
        qDebug() << "超时解析";
        thread->terminate();

    }
    m_Threads.clear();
}


// 实现 ThreadPool
Task::Task()
{
    qDebug()<<"task默认构造";
}

Task::~Task()
{
    qDebug()<<"task析构";
}

void Task::execute(int index)
{
    switch (index) {
    case 0:
    {
        if (ClientSocket->state() == QAbstractSocket::ConnectedState) {
            // msg=ui->lineEdit_14->text();
            QJsonObject jsonObject;
            jsonObject["ParentID"] = ParentID;
            jsonObject["SubID"] = SubID;
            jsonObject["Data"] = Data;
            // 将JSON对象转换为JSON文档
            QJsonDocument jsonDocument(jsonObject);

            // 将JSON文档转换为格式化的JSON字符串
            QByteArray jsonByteArray = jsonDocument.toJson(QJsonDocument::Indented);
            jsonByteArray+='end\n';
            ClientSocket->write(jsonByteArray);
        } else {
            qDebug() << "未建立连接，请先建立连接。";
            // 执行连接建立的逻辑，或者给出错误提示

        }
        break;
    }
    }
}
